Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: allow unwrapping of x as string expr #22953

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Delta456
Copy link
Member

@Delta456 Delta456 commented Nov 23, 2024

@@ -145,3 +145,27 @@ fn (mut g Gen) dot_or_ptr(val_type ast.Type) string {
'.'
}
}

@[inline]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is too long for @[inline]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right

@@ -7801,7 +7782,17 @@ fn (mut g Gen) as_cast(node ast.AsCast) {
g.as_cast_type_names[idx] = variant_sym.name
}
} else {
g.expr(node.expr)
if node.expr is ast.Ident {
if node.expr.info is ast.IdentVar {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if it is not a IdentVar?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if ast.IdentFn needs to unwrap the option type as this already works

fn get_opt_fn(a int) ?fn(int) int {
	if a < 0 {
		return none
	}
	return fn (n int) int {
		return n + n
	}
}

fn main() {
	d := get_opt_fn(12)
	dump(d as fn(int) int)
	
	e := d as fn(int) int
	dump(e(12))
	
}

though dump((d as fn(int) int)(12)) doesn't work due to the parser error. I don't think this would be valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C error on Option casting in assert statement
2 participants